
# -*- coding: utf-8 -*-
#
# Collector, the Audio CD collection manager
# Copyright (C) 2006 Piotr Gaczkowski
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import xml.dom

class Serializer:
  def __init__(self, data):
    self.data = data

  def write(self):
    dom = xml.dom.getDOMImplementation()
    doc = dom.createDocument(None, 'audio', None)
    root = doc.documentElement
    for d in self.data.discs:
      e = doc.createElement('album')
      for i in d.info:
        el = e.appendChild(doc.createElement('discid'))
        el.appendChild(doc.createTextNode(d.id))
        e.setAttribute('id', i['id'])
        artist = e.appendChild(doc.createElement('artist'))
        artist.setAttribute('artistid', i['artist']['id'])
        el = artist.appendChild(doc.createElement('name'))
        el.appendChild(doc.createTextNode(i['artist']['name']))
        el = artist.appendChild(doc.createElement('sortname'))
        el.appendChild(doc.createTextNode(i['artist']['sortName']))
        if i['asin'] is not None:
          el = e.appendChild(doc.createElement('asin'))
          el.appendChild(doc.createTextNode(i['asin']))
        el = e.appendChild(doc.createElement('title'))
        el.appendChild(doc.createTextNode(i['title']))
        if i['releaseEvents'] != []:
          rel = e.appendChild(doc.createElement('releaseEvents'))
        for r in i['releaseEvents']:
          date = rel.appendChild(doc.createElement('date'))
          date.appendChild(doc.createTextNode(r['date']))
          date.setAttribute('country', r['country'])
        tr = e.appendChild(doc.createElement('tracks'))
        for t in i['tracks']:
          track = tr.appendChild(doc.createElement('track'))
          track.setAttribute('duration', str(t['duration']))
          if t['artist'] is not None:
            a = track.appendChild(doc.createElement('artist'))
            a.setAttribute('artistid', i['artist']['id'])
            a.appendChild(doc.createTextNode(t['artist']['name']))
          a = track.appendChild(doc.createElement('title'))
          a.appendChild(doc.createTextNode(t['title']))
        if i['types'] != []:
          typ = e.appendChild(doc.createElement('types'))
        for t in i['types']:
          tt = typ.appendChild(doc.createElement('type'))
          tt.setAttribute('ns', t)
      root.appendChild(e)
    print doc.toprettyxml().encode('utf8')
